(function($) {
    /**
     * This plugin manages the display of notification popup messages. It displays, hides, and positions notifications
     * as needed.
     *
     * @author Josef van Niekerk <josef.van.niekerk@gmail.com>
     */
    jQuery.fn.notify = function(options) {
        return this.each(function() {
            var element = $(this);
            if (element.data('notifyplugindata')) return;
            var plugin = new Notifier(this);
            element.data('notifyplugindata', plugin);
            plugin.initialize(options);
        });
    };

    /**
     * Notifier plugin class
     */
    var Notifier = function(element) {
        /**
         * this scope for plugin.
         */
        var $this = this;

        /**
         * Element applying to plugin instance.
         */
        var $element = $(element);

        /**
         * Configuration options for plugin.
         */
        var settings = {

        }

        /**
         * initialize
         *
         * @param options
         *
         * @return $this
         */
        this.initialize = function(options) {
            if(options.type == "error") {
                this.addNotifier('error');
            } else {
                this.addNotifier('info');
            }
            return this;
        }

        /**
         * Adds a new notifier to the DOM.
         *
         * @param className Postfix class name.
         */
        this.addNotifier = function(className)
        {
            var offsetHeight = 40;
            $(".jq-notifier").each(function()
            {
                var bottom = parseInt($(this).css('top')) + $(this).outerHeight();
                offsetHeight = Math.max(offsetHeight, bottom);
            });
            $content = $('<div class="jq-notifier jq-notifier-' + className + '">' + $element.html() +
                '<span class="jq-notifier-icon"></span><span class="jq-notifier-close"></span></div>')
                .css('opacity', 0.9);

            // Add each notifier after the last notifier, or the beginning of the body.
            if($('.jq-notifier').length == 0) {
                $('body').prepend($content);
            } else {
                $($('.jq-notifier').last()).after($content);
            }

            $content
                .css({
                    top: offsetHeight > 0 ? offsetHeight + parseInt($content.css('marginTop')) : 0,
                    //left : $(window).width() / 2 - $content.outerWidth() / 2,
                    right:0,
                    opacity: 0
                })
                .animate({opacity: 0.9}, {duration: 'fast'});
            $('.jq-notifier-close', $content).bind('click', this.onClickClose);
        }

        /**
         * Click handler for the notifier close button.
         */
        this.onClickClose = function()
        {
            var $closing = $($(this).parent().get(0)); // The notifier being closed.
            $closing.fadeOut("normal", function(){ $(this).remove() });

            // Animate other visible notifiers into position.
            var shiftHeight = $closing.outerHeight() + parseInt($closing.css('marginTop'));
            var notifiers = $('.jq-notifier');
            for(var i = notifiers.length - 1; i >= 0; i--) {
                if(notifiers[i] == $closing.get(0)){
                    break;
                }
                $(notifiers[i]).animate({top: '-=' + shiftHeight + 'px'});
            }
        }
    };
})(jQuery);